package com.yuneec.droneservice.command;

import com.yuneec.droneservice.model.Aircraft;
import com.yuneec.droneservice.request.IResponse;
import com.yuneec.droneservice.utils.ByteUtils;

import java.util.Arrays;

/**
 * Created by gjw on 2017/3/27.
 */

public class ScanAircraft extends BaseCommand {
    public ScanAircraft() {
        super(MAV_RC_COMMAND.MAV_RC_CMD_SCAN_AUTOPILOT);
    }

    @Override
    public IResponse<Aircraft[]> toResponse(byte[] data, int off, int len) {
        int code = data[off + 1] & 0xff;
        IResponse<Aircraft[]> response = new IResponse(ids(), code);
        if (code == MAV_RC_ERROR_CODE.MAV_RC_ERR_SUCCESS) {
            int count = data[off + 2] & 0xff;
            if (count > 0) {
                Aircraft[] aircrafts = new Aircraft[count];
                for (int i = 0; i < count; i++) {
                    Aircraft aircraft = new Aircraft();
                    aircraft.setId(ByteUtils.byteArrayToInt(Arrays.copyOfRange(data, off + 3 + 4 * i, off + 3 + 4 * i + 4), IS_BIGENDIAN));
                    aircrafts[i] = aircraft;
                }
                response.setParam(aircrafts);
            }
        }
        return response;
    }
}
